//
//  VRSafetyTestingCell.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/22.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRSafetyTestingCell.h"

#import "VRSafetyTestingModel.h"
#import "NSString+Tools.h"

@interface VRSafetyTestingCell()

@property (nonatomic, assign) UIView *bottomLineView;
@property (nonatomic, weak) UILabel *title0Lbl;
@property (nonatomic, weak) UILabel *title1Lbl;
@property (nonatomic, weak) UILabel *title2Lbl;
@property (nonatomic, strong) UILabel *timeLbl;
@property (nonatomic, weak) UILabel *statusLbl;


@end

@implementation VRSafetyTestingCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"VRSafetyTestingCell";
    
    VRSafetyTestingCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[VRSafetyTestingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UILabel *title0Lbl = [[UILabel alloc] init];
    title0Lbl.text = @"巡更目标：";
    title0Lbl.textColor = RGB(51, 51, 51);
    title0Lbl.font = VRFont(16);
    [self.contentView addSubview:title0Lbl];
    self.title0Lbl = title0Lbl;
    
    UILabel *title1Lbl = [[UILabel alloc] init];
    title1Lbl.text = @"巡更低点：";
    title1Lbl.textColor = RGB(51, 51, 51);
    title1Lbl.font = VRFont(16);
    [self.contentView addSubview:title1Lbl];
    self.title1Lbl = title1Lbl;
    
    UILabel *title2Lbl = [[UILabel alloc] init];
    title2Lbl.text = @"巡更组：";
    title2Lbl.textColor = RGB(51, 51, 51);
    title2Lbl.font = VRFont(16);
    [self.contentView addSubview:title2Lbl];
    self.title2Lbl = title2Lbl;

    UILabel *signalLbl = [[UILabel alloc] init];
    signalLbl.textAlignment = NSTextAlignmentCenter;
    signalLbl.layer.borderWidth = 0.5;
    signalLbl.layer.borderColor = RGB(200, 200, 200).CGColor;
    signalLbl.textColor = RGB(51, 51, 51);
    signalLbl.font = VRFont(14);
    [self.contentView addSubview:signalLbl];
    self.statusLbl = signalLbl;
    
    UILabel *timeLbl = [[UILabel alloc] init];
    timeLbl.text = @"巡更组：";
    timeLbl.textColor = RGB(160, 160, 160);
    timeLbl.font = VRFont(14);
    [self.contentView addSubview:timeLbl];
    self.timeLbl = timeLbl;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.title0Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    [self.title1Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(-20);
    }];
    
    [self.title2Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
        make.right.mas_equalTo(self.timeLbl.mas_left).offset(-4);
    }];
    
    [self.timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.statusLbl.mas_left).offset(-2);
        make.centerY.mas_equalTo(self.title2Lbl);
    }];
    
    [self.statusLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.title2Lbl);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(24);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
}


#pragma mark - setter && getter
- (void)setModel:(VRSafetyTestingModel *)model
{
    _model = model;
    
    self.title0Lbl.text = [NSString stringWithFormat:@"巡更目标：%@", model.fTaskName ? model.fTaskName : @""];
    self.title1Lbl.text = [NSString stringWithFormat:@"巡更地点：%@", model.address ? model.address : @""];
    self.title2Lbl.text = [NSString stringWithFormat:@"巡更组：%@", model.fShiftsName ? model.fShiftsName : @""];
    self.timeLbl.text = model.fTime;
    if ([model.fState isEqualToString:@"error"]) {
        self.statusLbl.text = [NSString stringWithFormat:@"巡更状态：未巡更"];
    }else{
        self.statusLbl.text = [NSString stringWithFormat:@"巡更状态：已巡更"];
    }
    
    CGSize size = [model.fTime sizeWithFont:self.timeLbl.font];
    [self.timeLbl mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(size.width + 4);
    }];
    
    //
    //    self.numberLbl.text = [NSString stringWithFormat:@"%03zd", model.number];
    //    self.titleLbl.text = model.fFacilityModelID;
    //
    //    NSDictionary *typeDict = @{@(0) : @{@"name": @"正常", @"color": RGB(78, 173, 57), @"icon": @"i_data_success"}, @(1) : @{@"name": @"报警", @"color": RGB(238, 11, 25), @"icon": @"i_data_error"}, @(3) : @{@"name": @"故障", @"color": RGB(251, 86, 31), @"icon": @"i_data_error"}};
    //    self.signalLbl.backgroundColor = typeDict[@(model.fState)][@"color"];
    //    self.signalLbl.text = typeDict[@(model.fState)][@"name"];
    //
    //    self.statusLbl.text = typeDict[@(model.fState)][@"name"];
    //    self.statusLbl.textColor = typeDict[@(model.fState)][@"color"];
    //
    //    self.statusImgView.image = [UIImage imageNamed:typeDict[@(model.fState)][@"icon"]];
    //
    //    if (model.fFacilitySwitch == 1) {
    //        self.contentView.backgroundColor = RGB(140, 140, 140);
    //        self.titleLbl.textColor = [UIColor whiteColor];
    //        self.signalLbl.hidden = YES;
    //
    //        self.statusLbl.text = @"关闭";
    //        self.statusLbl.textColor = [UIColor whiteColor];
    //        self.statusImgView.hidden = YES;
    //    }else{
    //        self.contentView.backgroundColor = RGB(255, 255, 255);
    //        self.titleLbl.textColor = RGB(51, 51, 51);
    //        self.signalLbl.hidden = NO;
    //        self.statusImgView.hidden = NO;
    //    }
}

@end
